


/*
'===============================================================================
'  Generated From - UsersFirst_BusinessObject_DAAB.vbgen -- Generated 9/3/2010 5:37:12 PM by MyGeneration Version # (1.3.0.3)
' 
'  Sync.cs
'  Namespace: PFSync
'  Database:pfsync
'  Table:sync
'  Columns:
'
		id_sync	int
		id_user	int
		sync_time	DateTime
		sync_items_h_v	string
		sync_items_p_f	string
'
'
'  
'===============================================================================
*/
using System;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

namespace PFSync
{
	public partial class Sync
	{
		#region < Private Member Variables >
		
		private int id_sync = int.MinValue;
		private int id_user = int.MinValue;
		private DateTime sync_time = DateTime.MinValue;
		private string sync_items_h_v = string.Empty;
		private string sync_items_p_f = string.Empty;

		#endregion
		
		#region < Constructors >
		public Sync()
		{
			//Empty Constructor
		}
		public Sync(int id)
		{
			this.RestoreById(id);
		}
		
		public Sync(IDataReader reader)
		{
			this.ProcessRecord(reader);
		}
		public Sync(IDictionary dict)
		{
			this.ProcessDictionary(dict);
		}

		#endregion
		
		#region < Properties >
				
		public int IdSync
		{
			get	{ return id_sync; }
			set	{ id_sync = value; }
		}
				
		public int IdUser
		{
			get	{ return id_user; }
			set	{ id_user = value; }
		}
				
		public DateTime SyncTime
		{
			get	{ return sync_time; }
			set	{ sync_time = value; }
		}
				
		public string SyncItemsHV
		{
			get	{ return sync_items_h_v; }
			set	{ sync_items_h_v = value; }
		}
				
		public string SyncItemsPF
		{
			get	{ return sync_items_p_f; }
			set	{ sync_items_p_f = value; }
		}
				
		#endregion
		private void ProcessDictionary(IDictionary dict)
		{
			if(dict.Contains("idSync"))
				id_sync = int.Parse(dict["idSync"].ToString());
			if(dict.Contains("idUser"))
				id_user = int.Parse(dict["idUser"].ToString());
			if(dict.Contains("syncTime"))
				sync_time = DateTime.Parse(dict["syncTime"].ToString());
			if(dict.Contains("syncItemsHV"))
				sync_items_h_v = (string)dict["syncItemsHV"];
			if(dict.Contains("syncItemsPF"))
				sync_items_p_f = (string)dict["syncItemsPF"];
		
		}
		private void ProcessRecord(IDataReader reader)
		{
		
			id_sync = (int)reader["id_sync"];
			id_user = (int)reader["id_user"];
			sync_time = (DateTime)reader["sync_time"];
			sync_items_h_v = (string)reader["sync_items_h_v"];
			sync_items_p_f = (string)reader["sync_items_p_f"];
		
		}


		public void RestoreById(int id)
		{
			Database db = DatabaseFactory.CreateDatabase("pfsync_Connection");

			
			string sqlCommand = "SELECT * from sync WHERE id_sync=@id_sync";
			
			DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
			
			// Add in parameters
			db.AddInParameter(dbCommand, "@id_sync", DbType.Int32, id);

			IDataReader myReader = db.ExecuteReader(dbCommand);
			if (myReader.Read())
			{
				this.ProcessRecord(myReader);
			}
			myReader.Close();
			dbCommand.Connection.Close();		
		}
		public static List<Sync> RestoreAll()
		{
			Database db = DatabaseFactory.CreateDatabase("pfsync_Connection");

			string sqlCommand = "SELECT * from sync";
			DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
			IDataReader myReader = db.ExecuteReader(dbCommand);
			List<Sync> all = new List<Sync>();
			while (myReader.Read())
			{
				all.Add(new Sync(myReader));
			}
			myReader.Close();
			dbCommand.Connection.Close();
			return all;		
		}
		
		
		public static List<Sync> RestoreSyncListByIdUser(int id_user)
		{
			Database db = DatabaseFactory.CreateDatabase("pfsync_Connection");

			string sqlCommand = "SELECT * from sync where id_user =  " + id_user;
			DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
			IDataReader myReader = db.ExecuteReader(dbCommand);
			List<Sync> all = new List<Sync>();
			while (myReader.Read())
			{
				all.Add(new Sync(myReader));
			}
			myReader.Close();
			dbCommand.Connection.Close();
			return all;					
		}
			
		
		public void delete()
		{		
				Database db = DatabaseFactory.CreateDatabase("pfsync_Connection");
			if(id_sync != int.MinValue){
				string sqlCommand = "DELETE from sync WHERE id_sync=@id_sync";
	
				DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
			
			// Add in parameters
				db.AddInParameter(dbCommand, "@id_sync", DbType.Int32, id_sync);

				
				IDataReader reader = db.ExecuteReader(dbCommand);
				reader.Close();
				dbCommand.Connection.Close();
			}
			id_sync = int.MinValue;
			id_user = int.MinValue;
			sync_time = DateTime.MinValue;
			sync_items_h_v = string.Empty;
			sync_items_p_f = string.Empty;

		}
		public void save()
		{

			Database db = DatabaseFactory.CreateDatabase("pfsync_Connection");

			string insertCommand = "set nocount on; INSERT INTO sync(id_user, sync_time, sync_items_h_v, sync_items_p_f) VALUES (@id_user, @sync_time, @sync_items_h_v, @sync_items_p_f); select @@IDENTITY as ReturnInt";

			string updateCommand = "UPDATE sync SET id_user=@id_user, sync_time=@sync_time, sync_items_h_v=@sync_items_h_v, sync_items_p_f=@sync_items_p_f WHERE id_sync=@id_sync";


				DbCommand dbCommand;
				string sqlCommand = "";

			if(this.IdSync > 0){
				sqlCommand = updateCommand;
			}else{
				
				sqlCommand = insertCommand;
								
			}
			
			dbCommand = db.GetSqlStringCommand(sqlCommand);	
			
			
			db.AddInParameter(dbCommand, "@id_sync", DbType.Int32, IdSync);
			
			db.AddInParameter(dbCommand, "@id_user", DbType.Int32, IdUser);
			
			db.AddInParameter(dbCommand, "@sync_time", DbType.DateTime, SyncTime);
			
			db.AddInParameter(dbCommand, "@sync_items_h_v", DbType.AnsiString, SyncItemsHV);
			
			db.AddInParameter(dbCommand, "@sync_items_p_f", DbType.AnsiString, SyncItemsPF);

			db.AddOutParameter(dbCommand, "ReturnInt", DbType.Int32, 50);
			
			IDataReader reader = db.ExecuteReader(dbCommand);
			
			if(reader.Read() && this.IdSync <= 0){
				id_sync= int.Parse(reader["ReturnInt"].ToString());
			}
			dbCommand.Connection.Close();
		}
	}
}

